<?php
  if (!isset($_GET['op'])) {
    echo '参数错误，未授权的操作';
    return;
  }

require_once 'data/data.php';

$op = $_GET['op'];

$title = '新用户注册';
$action = 'message.php';
$email = '';
$username = '';
$age = '';
$gender = 1;
$coursesArr = ['','','','',''];
$coursesBase = ['html5','css','php','mysql','linux'];
$birth = '';
$attrs = [
  ['value' => 1,'text' => '中二青年'],
  ['value' => 2,'text' => '酷炫青年'],
  ['value' => 3,'text' => '文艺青年'],
  ['value' => 4,'text' => '普通青年']
];
$attr = 1;
$description = '';

if ($op == 2) {
  $title = '编辑用户';
  $action = 'message_edit.php';
  $id =  $_GET['id'];
  $sql = 'select id,email,user_name,(year(now())-year(birthday)) as age,gender,courses,birthday,attr,introduction from user where id=?';
  $user = query($sql,[$id])[0];

  $email = $user['email'];
  $username = $user['user_name'];
  $age = $user['age'];
  $gender = $user['gender'];
  $courses = explode(',', $user['courses']);
  for ($i=0; $i < count($courses); $i++) { 
    $coursesArr[$i] = trim($courses[$i]);
  }
  $birth = $user['birthday'];
  $attr = $user['attr'];
  $description = $user['introduction'];
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>信息页面</title>
</head>
<body>
  <form method="post" action="<?php echo $action ?>" target="_blank">
    <table width="500" border="1" align="center" cellspacing="0" cellpadding="2">
      <tr>
      <th align="center" colspan="2"><h1><?php echo $title ?></h1></th>
      </tr>
      <tr>
        <td align="right"><label for="email">邮&nbsp;&nbsp;&nbsp;箱：</label></td>
        <td><input type="email" id="email" name="email" placeholder="邮箱" value="<?=$email ?>"></td>
      </tr>
      <?php if ($op ==1):?>
      <tr>
        <td align="right"><label for="password">密&nbsp;&nbsp;&nbsp;码：</label></td>
        <td><input type="password" id="password" name="password" placeholder="密码"></td>
      </tr>
      <tr>
        <td align="right"><label for="pwd_again">再次输入密码：</label></td>
        <td><input type="password" id="pwd_again" name="pwd_again" placeholder="再次输入密码"></td>
      </tr>
      <?php endif;?>
      <tr>
        <td align="right"><label for="username">用户名：</label></td>
        <td><input type="text" id="username" name="username" placeholder="用户名" value="<?=$username ?>"></td>
      </tr>
      <tr>
        <td align="right"><label for="age"></label>年&nbsp;&nbsp;&nbsp;龄：</td>
        <td><input type="number" id="age" name="age" placeholder="年龄" value="<?=$age ?>"></td>
      </tr>
      <tr>
      	<td align="right"><label>性&nbsp;&nbsp;&nbsp;别：</label></td>
      	<td>
      	  <input type="radio" <?php if ($gender == 1): ?> checked = "checked" <?php endif ?> id="male" name="gender" value="1"><label for="male">男</label>
      	  <input type="radio" <?php if ($gender == 0): ?> checked = "checked" <?php endif ?> id="female" name="gender" value="0" ><label for="female">女</label>
      	</td>
      </tr>
      <tr>
        <td align="right"><label for="html5">主&nbsp;&nbsp;&nbsp;修：</label></td>
        <td>
          <?php foreach ($coursesBase as $c): ?>
            <input type="checkbox" id="<?=$c?>" name="course[]" value="<?=$c?>"
              <?php foreach ($coursesArr as $cc): ?>
                <?php if ($cc == $c): ?> checked="checked" <?php endif; ?> 
              <?php endforeach; ?> />
            <label for="<?=$c?>"><?=$c ?></label>
          <?php endforeach; ?>
        </td>
      </tr>
      <tr>
        <td align="right"><label for="birth">出生年月：</label></td>
        <td><input type="date" id="birth" name="birth" value="<?=$birth ?>"></td>
      </tr>
      <tr>
        <td align="right"><label for="attr">属&nbsp;&nbsp;&nbsp;性：</label></td>
        <td>
          <select id="attr" name="attr" >
            <?php foreach ($attrs as $item): ?>
              <option <?php if ($item['value'] == $attr): ?> selected="selected" <?php endif; ?> value="<?=$item['value'] ?>"><?=$item['text'] ?></option>
            <?php endforeach; ?>
          </select>
        </td>
      </tr>
      <tr>
        <td align="right"><label for="describ">自我介绍：</label></td>
        <td><textarea id="describ" name="describ" cols="50" rows="5"><?=$description ?></textarea></td>
      </tr>
      <tr >
      	<td align="center" colspan="2"><input type="submit" name="z"></a></td>
      </tr>
    </table>
    <?php if ($op == 2): ?>
    <input type="hidden" name="id" value="<?=$id ?>">
    <?php endif; ?>
  </form>
</body>
</html>